/**
 * 金蜘蛛战法，5 10 20 日均线汇聚一天，如果当天跳空高开的更好,前面8个交易日不能有放量，只能是盘整
 */
create or replace function getstjzz(days int) returns setof record as
$$
declare
rec record;
begin
  for rec in EXECUTE '
	select b."date",b.code,b.macd,
		s."name",s.industry from basic_data b  left join stocks s  on b.code=s.code 
	where 
	to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days||')
		and strpos(s."name",''ST'')=0
		and volume/vol5>=1.5
		and abs(ma10-ma5)<=0.01  and abs(ma10-ma20)<=0.01
		and ma20 < (select  ma20 from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+2||') and code=b.code ) 
		and (select abs(ma10-ma5) from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+1||') and code=b.code ) >0.01
		and (select abs(ma10-ma20) from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+1||') and code=b.code ) >0.01
		and macd != ''NaN''
		;
' loop
    return next rec;
  end loop;
return;
end
$$
language 'plpgsql';